package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.BookingsBusinessAddDTO;
import org.jsola.hr.dto.BookingsBusinessUpdateDTO;
import org.jsola.hr.query.BookingsBusinessQuery;
import org.jsola.hr.service.IBookingsBusinessService;
import org.jsola.hr.vo.BookingsBusinessListVO;
import org.jsola.hr.vo.BookingsBusinessVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 预约业务主表
 *
 * @author zhr
 */
@RestController("hrBookingsBusinessController")
@RequestMapping(HrConstants.API_PATH + "/v1/bookingsBusinesses")
public class BookingsBusinessController implements IBookingsBusinessControllerAPI {

    @Autowired
    private IBookingsBusinessService bookingsBusinessService;

    @Override
    @PostMapping
    public Result<BookingsBusinessVO> save(@Valid @RequestBody BookingsBusinessAddDTO bookingsBusinessAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增预约业务主表权限校验

        return Result.success(
                bookingsBusinessService.save(bookingsBusinessAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody BookingsBusinessUpdateDTO bookingsBusinessUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改预约业务主表权限校验

        int count = bookingsBusinessService.update(bookingsBusinessUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{bookingsBusinessId}")
    public Result<Integer> delete(@PathVariable Long bookingsBusinessId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除预约业务主表权限校验

        int count = bookingsBusinessService.deleteByIds(tokenUser, bookingsBusinessId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody Long[] bookingsBusinessIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除预约业务主表权限校验

        int count = bookingsBusinessService.deleteByIds(tokenUser, bookingsBusinessIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{bookingsBusinessId}")
    public Result<BookingsBusinessVO> findById(@PathVariable Long bookingsBusinessId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看预约业务主表权限校验

        return Result.success(
            bookingsBusinessService.selectById(bookingsBusinessId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<BookingsBusinessListVO>> listAll(@Valid BookingsBusinessQuery bookingsBusinessQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看预约业务主表权限校验

        return Result.success(
            bookingsBusinessService.select(bookingsBusinessQuery, "default")
        );
    }


    @Override
    @GetMapping
    public Result<Page<BookingsBusinessListVO>> page(@Valid BookingsBusinessQuery bookingsBusinessQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            bookingsBusinessService.selectPage(bookingsBusinessQuery, tokenUser.getSiteId())
        );
    }


}

